#---------------------------------------------------------
#
#
#----------------------------------------------------------

.PRECIOUS: %.pdf

FIGDIR=./figs
OBJPDF=./objpdf

# Le main
MAIN=lv6-ref-man

# Sources latex
SRCS= \
	$(MAIN).tex \
	preambule.tex \
	commandes.tex \
	exemple-env.tex \
	touch.tex

# targets: fig 2 latex 2 pdf
FIGS=\
	$(OBJPDF)/patate.tex \
	$(OBJPDF)/edge.tex \
	$(OBJPDF)/df.tex \
	$(OBJPDF)/map1.tex \
	$(OBJPDF)/red1.tex \
	$(OBJPDF)/fill1.tex \
	$(OBJPDF)/fillred1.tex


# targets: lus 2 latex
LUS2TEX= \
	$(OBJPDF)/speed.lus.github.tex \
	$(OBJPDF)/struct0.lus.tex \
	$(OBJPDF)/consensus2.lus.tex \
	$(OBJPDF)/count.lus.tex \
	$(OBJPDF)/EDGE.lus.tex \
	$(OBJPDF)/STABLE.lus.tex \
	$(OBJPDF)/TIME_STABLE.lus.tex \
	$(OBJPDF)/TIME_STABLE1.lus.tex \
	$(OBJPDF)/complex.lus.tex \
	$(OBJPDF)/COUNTER.lus.tex \
	$(OBJPDF)/SWITCH.lus.tex \
	$(OBJPDF)/SWITCH1.lus.tex \
	$(OBJPDF)/FALLING_EDGE.lus.tex \
	$(OBJPDF)/sincos.lus.tex \
	$(OBJPDF)/sincos-bad.lus.tex \
	$(OBJPDF)/integrator.lus.tex \
	$(OBJPDF)/type_decl.lus.tex \
	$(OBJPDF)/test_merge.lus.tex \
	$(OBJPDF)/enum0.lus.tex \
	$(OBJPDF)/param_node.lus.tex \
	$(OBJPDF)/param_node2.lus.tex \

# target: summary
SUMMARY=$(OBJPDF)/syntax_summary.tex


# target: mly 2 latex
PARSER=$(OBJPDF)/lv6parser.tex

-include ./Makefile.local

all: $(OBJPDF) $(MAIN).pdf lv6grammarlicence.tex 

.PHONY: dofigs
dofigs:  $(FIGS)

$(OBJPDF) :
	mkdir $(OBJPDF)

# 
$(OBJPDF)/syntax_summary.tex: $(SRCS)
	grep  newcommand $(SRCS) | grep "\\\syntax" | sed -e 's/{/\ /g' | sed -e 's/}/\ /g' | awk '{printf "%s\n", $$2}' > $@ 

#------------------------------
#------------------------------
h:$(SRCS)
	$(HOME)/i386-linux/hyperlatex $(SRCS)


#------------------------------
#------------------------------
$(MAIN).pdf : $(PARSER) $(SRCS) $(LUS2TEX) $(SUMMARY) $(FIGS)  $(OBJPDF)/version.tex
	cp $(SRCS) $(OBJPDF)
	cp *.sty  $(OBJPDF)
	(cd objpdf; pdflatex ../$(MAIN).tex   )
	mv objpdf/$(MAIN).pdf .

lv6grammarlicence.pdf: lv6grammarlicence.tex $(PARSER) $(OBJPDF)/version.tex
	cp $< $(OBJPDF)
	(cd objpdf; pdflatex ../$< )
	mv objpdf/$@ .

#------------------------------
# Special : fig 2 latex 2 pdf
#------------------------------
$(OBJPDF)/%.tex : $(FIGDIR)/%.fig
	cp $(FIGDIR)/$*.fig $(OBJPDF)/$*.fig
	fig2dev -Lpstex $(OBJPDF)/$*.fig > $(OBJPDF)/$*.ps
	cd $(OBJPDF); epstopdf $*.ps
	cd $(OBJPDF); fig2dev -Lpstex_t  -p $*.pdf $*.fig > $*.tex

LUS2LIC=lv6
$(OBJPDF)/version.tex:  Makefile
	rm -f lv6-ref-man/version.tex ; \
	date +VERSION_DATE=%d-%m-%y > lv6-ref-man/version.tex ; \
	echo "\\\newcommand{\\\version}{`$(LUS2LIC) --version | cut -d " " -f1`}" >  $(OBJPDF)/version.tex ; \
	echo "\\\newcommand{\\\sha}{`$(LUS2LIC) --version | cut -d " " -f2`}" >>  $(OBJPDF)/version.tex ; \
	echo "\\\newcommand{\\\versiondate}{`date +%d-%m-%y`}" >>  $(OBJPDF)/version.tex ;\




clean:
	rm -f $(OBJPDF)/*

#------------------------------
# Special : mly 2 latex
#------------------------------

BIN=/usr/local/tools/lustre/misc/bin


./$(OBJPDF)/lv6parser.tex:  ../lib/lv6parser.mly
	yacc2latex -t lustokens ../lib/lv6parser.mly -o $@ 

./$(OBJPDF)/cleanyacc : $(LUTINSRC)/lutParser.mly
	cat $< | sed -e "s/lus//g" -e "s/TK_EOF//" > $@ 


rere:
	touch ../lib/lv6parser.mly
	make lv6parser
	make re 
re:
	touch touch.tex
	make
	touch touch.tex
	make
#################################################
# direct inclusion of lustre non-reg files into the manual

#LUSTRE_DIR=$(HOME)//lus2lic/src/test/should_work
GITHUB_DIR=~/github/lustre-examples/verimag-v6/examples/
LUSTRE_DIR=../test/should_work
LUSTRE_DIR_bad=../test/should_fail
#LUS2TEX=$(HOME)/TeXinputs/lus2tex

# Generic rule for .lus.tex targets
# - All lus files must be in some subdir of LUSTRE_DIR
$(OBJPDF)/%-bad.lus.tex : $(LUSTRE_DIR_bad)/*/%.lus
	utils/lus2tex $< small > $@

$(OBJPDF)/%.lus.tex : $(LUSTRE_DIR)/%.lus
	utils/lus2tex $< small > $@

$(OBJPDF)/%.lus.github.tex : $(GITHUB_DIR)/%.lus
	utils/lus2tex $< scriptsize > $@

$(OBJPDF)/%-broken.lus.tex : $(LUSTRE_DIR)/broken/%.lus
	utils/lus2tex $< small > $@

lus2tex: $(LUS2TEX)

# obsolete
#lus2tex:
#	for f in ${EXEMPLES}; do \
#		$(LUS2TEX) $$f > `basename $$f`.tex ;\
#	done
#################################################
